/*
 * SiFiSy - A File System Simulator
 * Copyright (C) 2010  SevenSoft
 *
 * This file is part of SiFiSy.
 *
 * SiFiSy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SiFiSy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SiFiSy.  If not, see <http://www.gnu.org/licenses/>.
 */

package sifisy.core;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.output.XMLOutputter;
import java.io.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.*;
import com.itextpdf.text.pdf.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JRootPane;
import sifisy.newgui.*;

/**
 * Model class is the core of SiFiSy application.
 * Model provides the access at all functions of the software.
 * @author Giuseppe Biolo
 * @version %I%, %G%
 * @createData 06/03/2010
 */
public class Model {
    private static Vector<Simulation> simulations;
    private static Vector<Project> projects;
    private static String language;
    private static String schemaPath;
    private static final String setupPath = "./config/setup.xml";  // contain the setup file path
    private static String guiTranslatePath;
    private static String showWizard;
    private static String conceptualHelpPath;
    private static String technicalHelpPath;
    private static String defaultSimulationPath;

    /**
     * Load the main configuration file
     */
    public static void loadSetup() {
        simulations = new Vector<Simulation>();
        projects = new Vector<Project>();
        try {
            org.jdom.Document config = (new SAXBuilder()).build(setupPath);
            org.jdom.Element setupRoot = config.getRootElement();
            language = ((org.jdom.Element) (setupRoot.getChildren("language").
                    get(0))).getText();
            schemaPath = ((org.jdom.Element) (setupRoot.getChildren("schemaPath").
                    get(0))).getText();
            showWizard = ((org.jdom.Element) (setupRoot.getChildren("showWizard").
                    get(0))).getText();
            guiTranslatePath = ((org.jdom.Element) (setupRoot.
                    getChildren("guiTranslatePath").get(0))).getText();
            conceptualHelpPath = ((org.jdom.Element) (setupRoot.
                    getChildren("conceptualHelpPath").get(0))).getText();
            technicalHelpPath = ((org.jdom.Element) (setupRoot.
                    getChildren("technicalHelpPath").get(0))).getText();
            defaultSimulationPath = ((org.jdom.Element) (setupRoot.
                    getChildren("defaultSimulationPath").get(0))).getText();
        }
        catch (JDOMException e) {
        }
        catch (IOException e) {
        }
    }

    /**
     * Method that change setup with current configuration
     */
    public static void saveSetup() {
        org.jdom.Element setupRoot = new org.jdom.Element("setup");
        setupRoot.addContent("\n    ");
        setupRoot.addContent(new org.jdom.Element("language").addContent(language));
        setupRoot.addContent("\n    ");
        setupRoot.addContent(new org.jdom.Element("schemaPath").addContent(schemaPath));
        setupRoot.addContent("\n    ");
        setupRoot.addContent(new org.jdom.Element("showWizard").addContent(showWizard));
        setupRoot.addContent("\n    ");
        setupRoot.addContent(new org.jdom.Element("guiTranslatePath").addContent(guiTranslatePath));
        //setupRoot.addContent("\n    ");
        //setupRoot.addContent(new org.jdom.Element("getHelpPath").addContent(getHelpPath));
        setupRoot.addContent("\n    ");
        setupRoot.addContent(new org.jdom.Element("conceptualHelpPath").addContent(conceptualHelpPath));
        setupRoot.addContent("\n    ");
        setupRoot.addContent(new org.jdom.Element("technicalHelpPath").addContent(technicalHelpPath));
        setupRoot.addContent("\n    ");
        setupRoot.addContent(new org.jdom.Element("defaultSimulationPath").addContent(defaultSimulationPath));
        setupRoot.addContent("\n");
        try {
            XMLOutputter setup = new XMLOutputter();
            FileWriter writer = new FileWriter(setupPath);
            setup.output(setupRoot, writer);
            writer.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Methot that return the message identified by the parameter message and
     * language
     * @param message
     * @return
     */
    public static String translate(String message) {
        try {
            org.jdom.Document guiTrans = (new SAXBuilder()).build(guiTranslatePath);
            org.jdom.Element guiTransRoot = guiTrans.getRootElement();
            java.util.List results = guiTransRoot.getChildren(message);
            for (int i = 0; i < results.size(); i++) {
                if (((org.jdom.Element)(results.get(i))).getAttributeValue("lang").
                        equals(language)) {
                    return (((org.jdom.Element)(results.get(i))).getText());
                }
            }
            return "Unknown";
        } catch (JDOMException e) {
            return "JDOM exception";
        } catch (IOException e) {
            return "IO exception";
        }
    }

    /**
     * Method that return the message identified by the parameter message,
     * language and the path of XML file
     * @param message
     * @param helpFilePath
     * @return
     */
    public static String help(String message, String helpFilePath) {
        try {
            org.jdom.Document guiTrans = (new SAXBuilder()).build(helpFilePath);
            org.jdom.Element guiTransRoot = guiTrans.getRootElement();
            java.util.List results = guiTransRoot.getChildren(message);
            for (int i = 0; i < results.size(); i++) {
                if (((org.jdom.Element)(results.get(i))).getAttributeValue("lang").
                        equals(language)) {
                    return (((org.jdom.Element)(results.get(i))).getText());
                }
            }
            return "Unknown";
        } catch (JDOMException e) {
            return "JDOM exception";
        } catch (IOException e) {
            return "IO exception";
        }
    }

    /**
     * Method that call Help with the parameter message and the XML path of
     * conceptual help
     * @param message
     * @return
     */
    public static String conceptualHelp(String message) {
        return (help(message,conceptualHelpPath));
    }

     /**
     * Method that call Help with the parameter message and the XML path of
     * technical help
     * @param message
     * @return
     */
    public static String technicalHelp(String message) {
        return (help(message,technicalHelpPath));
    }

     /**
     * Create a new simulation and add it to the simulations list
     * @param Simulation type
     * @return Reference to the new simulation
     */
    public static void newSimulation(String name, SimulationPanel panel) {
        Simulation newSimulation = null;
        try {
            org.jdom.Document simulation = (new SAXBuilder()).build(defaultSimulationPath);
            org.jdom.Element simulationRoot = simulation.getRootElement();
            org.jdom.Element fileSystemRoot = (org.jdom.Element) (simulationRoot.getChild("fileSystem"));
            String type = (fileSystemRoot.getChild("type")).getText();
            if (type.equals("Ext2")) {
                newSimulation = new Ext2();
            }
            if (type.equals("Fat32")) {
                newSimulation = new Fat32();
            }
            if (type.equals("NTFS")) {
                newSimulation = new Ntfs();
            }
            newSimulation.open(simulationRoot);
            newSimulation.setSimName(name);
            simulations.add(newSimulation);
            panel.setId(simulations.size()-1);
        } catch (JDOMException e) {
            panel.notifyPanel("JDOM Exception");
        } catch (IOException e) {
            panel.notifyPanel("I/O Exception");
        }
    }

    /**
     * Open an existing simulation
     * @param path: complete path of the simulation file
     */
    public static void openSimulation(String path, SimulationPanel panel) {
        int simId = openSimulation(path);

        panel.notifyPanel(simId);
    }

    /**
     * Open an existing simulation
     * @param path: complete path of the simulation file
     */
    static int openSimulation(String path) {
        Simulation newSimulation = null;
        int simId = -1;
        try {
            org.jdom.Document simulation = (new SAXBuilder()).build(path);
            org.jdom.Element simulationRoot = simulation.getRootElement();
            org.jdom.Element fileSystemRoot = (org.jdom.Element) (simulationRoot.getChild("fileSystem"));
            String type = (fileSystemRoot.getChild("type")).getText();
            if (type.equals("Ext2")) {
                newSimulation = new Ext2();
            }
            if (type.equals("Fat32")) {
                newSimulation = new Fat32();
            }
            if (type.equals("NTFS")) {
                newSimulation = new Ntfs();
            }
            newSimulation.open(simulationRoot);
            simulations.add(newSimulation);
            simId = simulations.size() - 1;
        }
        catch (org.jdom.JDOMException e) {
            System.err.println(e.getMessage());
        }
        catch (java.io.IOException e) {
            System.err.println(e.getMessage());
        }
        finally {
            return simId;
        }
    }

    /**
     * Method that open an existent simulation
     * @param simulationRoot
     * @param panel
     */
    private static void openSimulation(org.jdom.Element simulationRoot,
        SimulationPanel panel) {
        Simulation newSimulation = null;

        org.jdom.Element fileSystemRoot = (org.jdom.Element)(simulationRoot.getChild("fileSystem"));
        String type = (fileSystemRoot.getChild("type")).getText();
        System.out.println(type);
        if (type.equals("Ext2")) {
            newSimulation = new Ext2();
        }
        if (type.equals("Fat32")) {
            newSimulation = new Fat32();
        }
        if (type.equals("NTFS")) {
            newSimulation = new Ntfs();
        }
        newSimulation.open(simulationRoot);
        simulations.add(newSimulation);
        panel.notifyPanel(simulations.size() - 1);
    }

    /**
     * Methot that set all the simulation paramiters to default
     * @param simulationId
     * @param panel
     */
    public void resetToDefault(int simulationId, SimulationPanel panel) {
        try {
            org.jdom.Document simulation = (new SAXBuilder()).build(defaultSimulationPath);
            org.jdom.Element simulationRoot = simulation.getRootElement();
            Simulation newSimulation = new Ext2();
            newSimulation.open(simulationRoot);
            simulations.set(simulationId, newSimulation);
            //simulations.elementAt(simulationId).open(simulationRoot);
            panel.notifyPanel();
        } catch (JDOMException e) {
            panel.notifyPanel("JDOM Exception");
        } catch (IOException e) {
            panel.notifyPanel("I/O Exception");
        }
    }

    /**
     * Switch the simulation at the simulationId index of simulations
     * from its present filesystem type to the one received, by creating a new
     * simulation of the required type and giving at the old simulation of
     * index simulationId this new simulaiton, the old one will be erased by the
     * garbage collector
     * @param simulationId
     * @param fileSystemType
     */
    public void switchFileSystem(int simulationId, String fileSystemType) {
        Simulation newSim = null;
        Simulation oldSim = simulations.elementAt(simulationId);

        if (fileSystemType.equals("EXT2")){
            newSim = new Ext2();
            ((Ext2)newSim).setInodeDimension(128);
            ((Ext2)newSim).setAddressLevel(3);
            ((Ext2)newSim).setFirstInodeDirectBlock(12);
        }

        if (fileSystemType.equals("FAT32")){
            newSim = new Fat32();
        }

        if (fileSystemType.equals("NTFS")){
             newSim = new Ntfs();
             ((Ntfs)newSim).setBaseRecord(400);
             ((Ntfs)newSim).setExtensionRecord(800);
        }
       
        newSim.setFiles(oldSim.getFiles());
        newSim.setUsedBlocks(oldSim.getUsedBlocks());
        newSim.setBlockDimension(oldSim.getBlockDimension());
        newSim.setBlockExponent(oldSim.getBlockExponent());

        newSim.setPartitionDimension(oldSim.getPartitionDimension());
        newSim.setPartitionExponent(oldSim.getPartitionExponent());

        newSim.setAddressDimension(oldSim.getAddressDimension());
        newSim.setFileAllocationType(oldSim.getFileAllocationType());
        newSim.setAllocationNextIndex(oldSim.getAllocationNextIndex());

        newSim.setRotationSpeed(oldSim.getRotationSpeed());
        newSim.setTrackPercent(oldSim.getTrackPercent());
        newSim.setSeekTime(oldSim.getSeekTime());
//        newSim.setTrackSectors(oldSim.getTrackSectors());

        newSim.setSimName(oldSim.getSimName());

        simulations.set(simulationId, newSim);
    }

    /**
     * Save a simulation contained in the simulations vector
     * @param index: index of the simulation to save
     * @param path: complete path of the destination file
     */
    public static void saveSimulation(int index, String path, SimulationPanel panel ) {
        org.jdom.Element simulationRoot = new org.jdom.Element("simulation");
        simulations.elementAt(index).save(simulationRoot);
        try {
            XMLOutputter simulation = new XMLOutputter();
            FileWriter writer = new FileWriter(path);
            simulation.output(simulationRoot, writer);
            writer.close();
            panel.notifyPanel();
        } catch (java.io.IOException e) {
            panel.notifyPanel("I/O Exception");
        }
    }

    /**
     * Method that save simulation identified with simId as default simulation
     * @param index
     * @param panel
     */
    public static void saveDefaultSimulation(int simId, SimulationPanel panel) {
        saveSimulation(simId,defaultSimulationPath,panel);
    }

/**
 * Method that call start method of the simulation identified with simId
 * @param simId
 * @param panel
 */
    public static void startSimulation(int simId, SimulationPanel panel) {
        (simulations.elementAt(simId)).start();
        panel.notifyPanel();
        //panel.showResults();
    }

    /**
     * Metod that remove a silmulation open in SiFiSy
     * @param simulationId
     */
    public static void removeSimulation(int simulationId) {
        simulations.set(simulationId,null);
    }

    /**
     * Method that export a grafic of simulation as file PNG
     * @param panel
     * @param simPath
     * @throws java.io.FileNotFoundException
     */
    public static void exportSimulationPng(JRootPane panel,
            String simPath) throws java.io.FileNotFoundException {

        final BufferedImage image = new BufferedImage(
                       panel.getWidth(), panel.getHeight(),
                       BufferedImage.TYPE_INT_ARGB);

        Graphics gr = image.getGraphics();
        panel.printAll(gr);
        gr.dispose();
        try {
            if(simPath.indexOf(".png") == simPath.length() - 4)
        
                ImageIO.write(image, "PNG",
                    new java.io.File(simPath));
            else
                ImageIO.write(image, "PNG",
                    new java.io.File(simPath + ".png"));
        }
        catch (IOException ex) {
            Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Method that export a simulation result as file PDF
     * @param panel
     * @param simPath
     * @throws com.itextpdf.text.DocumentException
     */
    public static void exportSimulationPdf(SimulationPanel panel, String simPath) throws
            com.itextpdf.text.DocumentException {
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        try {
            PdfWriter writer;

            //String simName = panel.getSimulationName();
            if(simPath.indexOf(".pdf") != simPath.length() - 4)
                simPath = simPath + ".pdf";
            
            writer = PdfWriter.getInstance(document,
            new FileOutputStream(simPath));
            
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate( panel.getWidth(),
                    panel.getHeight() );
            Graphics2D g2;
           
            g2 = tp.createGraphicsShapes( panel.getWidth(), panel.getTableHeight() );
           
            panel.getResults().print(g2);
            g2.dispose();
            cb.addTemplate(tp, 60, 500);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
        
        panel.notifyPanel();
    }

    /**
     * Method that export a project result as file PDF
     * @param panel
     * @param prjPath
     * @throws com.itextpdf.text.DocumentException
     */
    public static void exportProjectPdf(ProjectPanel panel, String prjPath) throws
            com.itextpdf.text.DocumentException {
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        try {
            PdfWriter writer;

            //String simName = panel.getSimulationName();
            if(prjPath.indexOf(".pdf") != prjPath.length() - 4)
                prjPath = prjPath + ".pdf";
            writer = PdfWriter.getInstance(document,
            new FileOutputStream(prjPath));

            document.open();
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate( panel.getWidth(),
                    panel.getHeight() );
            Graphics2D g2;

            g2 = tp.createGraphicsShapes(panel.getWidth(), panel.getTableHeight() );

            AffineTransform scale = g2.getTransform();
            scale.scale(0.35, 0.35);

            g2.setTransform(scale);

            panel.getResults().print(g2);
            g2.dispose();
            cb.addTemplate(tp, 60, 500);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();

        panel.notifyPanel();
    }

    /**
     * Create a new project and add it to the projects list
     * @param The new project name
     * @return Reference to the new simulation
     */
    public static void newProject(String name, ProjectPanel panel) {
        Project newProject = new Project(name);
        projects.add(newProject);
        panel.notifyPanel(projects.size() - 1);
    }

    /**
     * Create a new default simulation, and add its simulationId to the project
     * @param simName
     * @param panel
     */
    public static void addSimulation(String simName, SimulationPanel panel, int projectId){
       newSimulation(simName, panel);
       Integer simId = simulations.size() - 1;
       projects.get(projectId).addSimulation(simId);
    }

    /**
     * Open an existing project from a specified file
     * @param path: complete path of the source file
     */
    public static void openProject(String path, ProjectPanel panel) {
        Project newProject = new Project();
        try {
            org.jdom.Document project = (new SAXBuilder()).build(path);
            org.jdom.Element projectRoot = project.getRootElement();
            newProject.open(projectRoot);
            projects.add(newProject);
            panel.setProjectId(projects.size() - 1);

            List simulations = projectRoot.getChildren("simulation");
            for (int i=0; i<simulations.size(); i++) {
                org.jdom.Element sim = (org.jdom.Element)(simulations.get(i));
                openSimulation(sim, panel.addSimulation(sim.getChild("simName").getText(),
                        new SifisyTreeNode(sim.getChild("simName").getText())));
            }

            panel.notifyPanel(projects.size() - 1);
            //view.notifyView(null);
            
        }
        catch (JDOMException e) {
            //panel.notifyPanel("JDOM Exception");
        } 
        catch (IOException e) {
            //panel.notifyPanel("I/O Exception");
        }
    }

    /**
     * Remove a specified project and its simulations for the vectors
     * @param panel
     * @param projectId
     */
    public static void removeProject(int projectId) {
        for (int i=0; i < (projects.elementAt(projectId)).getSimulationsRef().size(); i++) {
            removeSimulation((projects.elementAt(projectId)).getSimulationsRef().elementAt(i));
        }
        projects.setElementAt(null, projectId);
    }

    /**
     * Save a project contained in the projects vector
     * @param index: index of the project to save
     * @param path: complete path of the destination file
     */
    public static void saveProject(int prjId, String path, ProjectPanel panel) {
        org.jdom.Element projectRoot = new org.jdom.Element("project");
        (projects.elementAt(prjId)).save(projectRoot);
        try {
            XMLOutputter project = new XMLOutputter();
            FileWriter writer = new FileWriter(path);
            project.output(projectRoot, writer);
            writer.close();
            //view.notifyView(null);
            //panel.notifyPanel();
        } catch (java.io.IOException e) {
            //panel.notifyPanel("I/O Exception");

        }
    }

    /**
     * Method that set the value of parameter Block in simulation
     * identified by simId
     * @param simId
     * @param block
     */
    public void setBlock(int simId, int block) {
        (simulations.elementAt(simId)).setBlockDimension(block);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter BlockExponent in simulation
     * identified by simId
     * @param simId
     * @param exponent
     */
    public void setBlockExponent(int simId, int exponent){
        (simulations.elementAt(simId)).setBlockExponent(exponent);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter Partition in simulation
     * identified by simId
     * @param simId
     * @param partition
     */
    public void setPartition(int simId, int partition) {
        (simulations.elementAt(simId)).setPartitionDimension(partition);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter PartitionExponent in simulation
     * identified by simId
     * @param simId
     * @param exponent
     */
    public void setPartitionExponent(int simId, int exponent){
        (simulations.elementAt(simId)).setPartitionExponent(exponent);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter InodeDimension in simulation
     * identified by simId
     * @param simId
     * @param inode
     */
    public void setInodeDimension(int simId, int inode) {
        ((Ext2)(simulations.elementAt(simId))).setInodeDimension(inode);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter BlockFirstInode in simulation
     * identified by simId
     * @param simId
     * @param blockFirstInode
     */
    public void setBlockFirstInode(int simId, int blockFirstInode) {
        ((Ext2)(simulations.elementAt(simId))).setFirstInodeDirectBlock(blockFirstInode);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter MaxAddressLevel in simulation
     * identified by simId
     * @param simId
     * @param maxAddressLevel
     */
    public void setMaxAddressLevel(int simId, int maxAddressLevel) {
        ((Ext2)(simulations.elementAt(simId))).setAddressLevel(maxAddressLevel);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter Track in simulation
     * identified by simId
     * @param simId
     * @param track
     */
    public void setTrack(int simId, int track){
        ((simulations.elementAt(simId))).setTrackPercent(track);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

//    /**
//     * Method that set the value of parameter BlockFileTrack in simulation
//     * identified by simId
//     * @param simId
//     * @param seekTime
//     */
//    public void setBlockFileTrack(int simId, int blockFileTrack){
//        ((simulations.elementAt(simId))).setTrackSectors(blockFileTrack);
//        (simulations.elementAt(simId)).setModifiedCamps(true);
//    }

    /**
     * Method that set the value of parameter SeekTime in simulation
     * identified by simId
     * @param simId
     * @param seekTime
     */
    public void setSeekTime(int simId, int seekTime){
        ((simulations.elementAt(simId))).setSeekTime(seekTime);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter RotationSpeed in simulation
     * identified by simId
     * @param simId
     * @param rotationSpeed
     */
    public void setRotationSpeed(int simId, int rotationSpeed){
        ((simulations.elementAt(simId))).setRotationSpeed(rotationSpeed);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

/**
 * Method that set the value of parameter MainRecord in simulation
     * identified by simId
 * @param simId
 * @param recordSize
 */
    public void setMainRecord(int simId, int recordSize) {
        ((Ntfs)(simulations.elementAt(simId))).setBaseRecord(recordSize);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter ExtensionRecord in simulation
     * identified by simId
     * @param simId
     * @param extensionSize
     */
    public void setExtensionRecord(int simId, int extensionSize) {
        ((Ntfs)(simulations.elementAt(simId))).setExtensionRecord(extensionSize);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set a single file in simulation identified by simId
     * @param simId
     * @param fileName
     * @param fileDimension
     * @param fileExponent
     * @param average
     */
    public void setFile(int simId, String fileName, int fileDimension, int fileExponent, int average){
        ((simulations.elementAt(simId)).getFiles()).clear();
        (simulations.elementAt(simId)).setFileAllocationType(4);
        File item = new File();
        item.setId(fileName);
        item.setDimension(fileDimension);
        item.setExponent(fileExponent);
        item.setAverageBlocks(average);
        (simulations.elementAt(simId)).addFile(item);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that add a file in simulation identified by simId with
     * multiple insert mode
     * @param simId
     * @param fileName
     * @param fileDimension
     * @param fileExponent
     */
    public void setMultiFile(int simId, String fileName, int fileDimension, int fileExponent){
        File item = new File();
        item.setId(fileName);
        item.setDimension(fileDimension);
        item.setExponent(fileExponent);
        long blockDim=simulations.elementAt(simId).getBlockDimension();
        double blockExp=simulations.elementAt(simId).getBlockExponent();
        double blockSize=(blockDim*Math.pow(2, blockExp));
        double fileSize=(fileDimension*Math.pow(2, fileExponent));
        long average=(long)Math.ceil(fileSize/ blockSize);
        item.setAverageBlocks(average);
        (simulations.elementAt(simId)).addFile(item);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter Advanced in simulation
     * identified by simId
     * @param simId
     * @param fileName
     */
    public static void setAdvanced(int simId, String fileName){
        simulations.elementAt(simId).getFile(fileName).setSelectedAdvanced(true);
    }

    
//    public static void setFileDimension(int simId, int fileDimension) {
//        //(simulations.elementAt(simId)).setFileDimension(fileDimension);
//        (simulations.elementAt(simId)).setModifiedCamps(true);
//    }
//
//    public static void setFileExponent(int simId, int exponent) {
//        //(simulations.elementAt(simId)).setFileExponent(exponent);
//        (simulations.elementAt(simId)).setModifiedCamps(true);
//    }

    /**
     * Method that delete a file in simulation identified by simId with
     * multiple insert mode
     * @param simId
     * @param fileName
     */
    public void deleteFile(int simId, String fileName) {
        (simulations.elementAt(simId)).deleteFile(fileName);
        (simulations.elementAt(simId)).setModifiedCamps(true);
    }

    /**
     * Method that set the value of parameter AllocationType in simulation
     * identified by simId
     * @param simId
     * @param allocationType
     */
    public void setAllocationType(int simId, String allocationType){
        int numAllocation=4;
        if(allocationType.equals("First-Fit"))
            numAllocation=0;
        if(allocationType.equals("Best-Fit"))
            numAllocation=1;
        if(allocationType.equals("Worst-Fit"))
            numAllocation=2;
        if(allocationType.equals("Next-Fit"))
            numAllocation=3;
        (simulations.elementAt(simId)).setFileAllocationType(numAllocation);
    }

    /**
     * Method that set the language choise by user
     * @param lang
     */
    public static void setLanguage(String lang) {
        language = lang;
        //changedSetup = true;
    }

    /**
     * Method thar return the language
     * @return
     */
    public static String getLanguage() {
        return language;
    }

    /**
     * Method that return the path of the XML schema
     * @return
     */
    public static String getSchemaPath() {
        return schemaPath;
    }

    /**
     * Method that return if when users start SiFiSy, wizard is showed
     * @param showWizard
     */
    public static String getShowWizard() {
        return showWizard;
    }

    /**
     * Method that return the path of the XML setup
     * @return
     */
    public static String getSetupPath() {
        return setupPath;
    }

    /**
     * Method that return the path of the XML translate
     * @return
     */
    public static String getGuiTranslatePath() {
        return guiTranslatePath;
    }

    /**
     * Method that return type of simulation identified by simId
     * @param simId
     * @return
     */
    public static String getFsType(int simId){
        String fs = null;
        if((simulations.elementAt(simId)) instanceof Ext2)
            fs = "EXT2";
        if((simulations.elementAt(simId)) instanceof Fat32)
            fs = "FAT32";
        if((simulations.elementAt(simId)) instanceof Ntfs)
            fs = "NTFS";
        return fs;
    }

    /**
     * Method that return the value of parameter MaxAddressLevel in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getMaxAddressLevel(int simId){
        return (simulations.elementAt(simId)).getAddressDimension();
    }

    /**
     * Method that return the value of parameter BlockDimension in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getBlockDimension(int simId){
        return (simulations.elementAt(simId)).getBlockDimension();
    }

    /**
     * Method that return the value of parameter BlockExponent in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getBlockExponent(int simId){
        return (simulations.elementAt(simId)).getBlockExponent();
    }

    /**
     * Method that return the value of parameter PartitionDimension
     * in simulation identified by simId
     * @param simId
     * @return
     */
    public static int getPartitionDimension(int simId){
        return (simulations.elementAt(simId)).getPartitionDimension();
    }

    /**
     * Method that return the value of parameter PartitionExponent in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getPartitionExponent(int simId){
        return (simulations.elementAt(simId)).getPartitionExponent();
    }

    /**
     * Method that return the value of parameter FileAllocationType 
     * in simulation identified by simId
     * @param simId
     * @return
     */
    public static int getFileAllocationType(int simId){
        return (simulations.elementAt(simId)).getFileAllocationType();
    }

    /**
     * Method that return the value of parameter AllocationNextIndex 
     * in simulation identified by simId
     * @param simId
     * @return
     */
    public static int getAllocationNextIndex(int simId){
        return (simulations.elementAt(simId)).getAllocationNextIndex();
    }

    /**
     * Method that return the value of parameter AddressDimension in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getAddressDimension(int simId){
        return (simulations.elementAt(simId)).getAddressDimension();
    }

    /**
     * Method that return the value of parameter InodeDimension in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getInodeDimension(int simId){
        return ((Ext2)(simulations.elementAt(simId))).getInodeDimension();
    }

    /**
     * Method that return the value of parameter BlockFirstInode in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getBlockFirstInode(int simId){
        return ((Ext2)(simulations.elementAt(simId))).getFirstInodeDirectBlock();
    }

    /**
     * Method that return the value of parameter AddressLevel in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getAddressLevel(int simId){
        return ((Ext2)(simulations.elementAt(simId))).getAddressLevel();
    }

    /**
     * Method that return the value of parameter DiskRotationSpeed in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getDiskRotationSpeed(int simId){
        return (simulations.elementAt(simId)).getRotationSpeed();
    }

    /**
     * Method that return the value of parameter TrackPercent in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getTrackPercent(int simId){
        return (simulations.elementAt(simId)).getTrackPercent();
    }

    /**
     * Method that return the value of parameter SeekTime in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getSeekTime(int simId){
        return (simulations.elementAt(simId)).getSeekTime();
    }

//    public static int getTrackSectors(int simId){
//        return (simulations.elementAt(simId)).getTrackSectors();
//    }

    //public static int getFramm(int simId){
    //    return (simulations.elementAt(simId)).getFramm();
    //}

    //public static int getFileDimension(int simId){
        //return (simulations.elementAt(simId)).getFileDimension();
    //}

    /**
     * Method that call simulation identified by simId to calculate
     * the size of max length file
     * @param simId
     */
    public static void calculateMaxLenghtFile(int simId){
        simulations.elementAt(simId).viewMaxFile();
    }

//    public static int getFileExponent(int simId){
//        return 1; //sim.getFileExponent();
//    }

    /**
     * Method that return the value of parameter MaxLenghtFile in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static long getMaxLenghtFile(int simId){
        return (simulations.elementAt(simId)).getMaxLenghtFile();
    }

    /**
     * Method that return the value of parameter InternalFragmentation
     * in simulation identified by simId
     * @param simId
     * @return
     */
    public static double getInternalFragmentation(int simId){
        return (simulations.elementAt(simId)).getInternalFragmentation();
    }

    /**
     * Method that return the value of parameter InflaxionIndex in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static double getInflationIndex(int simId){
        return (simulations.elementAt(simId)).getInflationIndex();
    }

    /**
     * Method that return the value of parameter AverageReading in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getAverageReading(int simId){
        return (simulations.elementAt(simId)).getAverageReading();
    }

    /**
     * Method that return the value of parameter MainRecord in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getMainRecord(int simId) {
        return ((Ntfs)(simulations.elementAt(simId))).getBaseRecord();
    }

    /**
     * Method that return the value of parameter ExtensionRecord in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static int getExtensionRecord(int simId) {
        return ((Ntfs)(simulations.elementAt(simId))).getExtensionRecord();
    }

    /**
     * Method that return the value of parameter SimulationName in simulation
     * identified by simId
     * @param simId
     * @return
     */
    public static String getSimulationName(int simId){
        return (simulations.elementAt(simId)).getSimName();
    }

    /**
     * Method that return a project identified by projectId
     * @param simId
     * @return
     */
    public static Project getProject(int projectId) {
        return projects.elementAt(projectId);
    }

    /**
     * Method that return the vectior of simulation
     * @param simId
     * @return
     */
    public static Vector<Simulation> getSimulations() {
        return simulations;
    }

    /**
     * Method that return a simulation identified by simId
     * @param simId
     * @return
     */
    public static Simulation getSimulation(int simId) {
        return (simulations.elementAt(simId));
    }

    /**
     * Method that set the paramiter ChooseAdvanced of a simulation
     * identified by simId
     * @param simId
     * @param chosed
     */
    public static void setChooseAdvanced(int simId, boolean chosed){
        simulations.elementAt(simId).setChooseAdvanced(chosed);
    }

    /**
     * Method that return the value of ChoosedAdvanced of a simulation
     * identified by simName
     * @param simId
     * @return
     */
    public static boolean getChooseAdvanced(int simId){
        return simulations.elementAt(simId).getChooseAdvanced();
    }

    /**
     * Method that return the path of XML DefaultSimualtion
     * @return
     */
    public static String getDefaultSimulationPath() {
        return defaultSimulationPath;
    }

    /**
     * Method that set if when users start SiFiSy, wizard is showed
     * @param showWizard
     */
    public static void setShowWizard(String showWizard) {
        Model.showWizard = showWizard;
    }

    /**
     * Method that set the path of default simulation
     * @param defaultSimulationPath
     */
    public static void setDefaultSimulationPath(String defaultSimulationPath) {
        Model.defaultSimulationPath = defaultSimulationPath;
    }

    /**
     * Method that set the path of schema
     * @param schemaPath
     */
    public static void setSchemaPath(String schemaPath) {
        Model.schemaPath = schemaPath;
    }

    /**
     * Method that set the path of the conceptual help
     * @param conceptualHelpPath
     */
    public static void setConceptualHelpPath(String conceptualHelpPath) {
        Model.conceptualHelpPath = conceptualHelpPath;
    }

    /**
     * Method that set the path of the Gui translate
     * @param guiTranslatePath
     */
    public static void setGuiTranslatePath(String guiTranslatePath) {
        Model.guiTranslatePath = guiTranslatePath;
    }

    /**
     * Method that set the path of the technical help
     * @param technicalHelpPath
     */
    public static void setTechnicalHelpPath(String technicalHelpPath) {
        Model.technicalHelpPath = technicalHelpPath;
    }

    public static Vector<Project> getProjects() {
        return projects;
    }
}
